/***********************************************
 This file is part of redia-lib-utils-ios
 
 
 Copyright (c) 2015 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

#import "RediaSecureTextFieldWithCustomFont.h"

@interface RediaSecureTextFieldWithCustomFont(){
    BOOL isOSSupported;
}

@end

@implementation RediaSecureTextFieldWithCustomFont

-(void) commonInit{
    NSString *versionString = [[UIDevice currentDevice] systemVersion];
    isOSSupported = [[[versionString componentsSeparatedByString:@"."] objectAtIndex:0] integerValue] >= 7;
}

-(id)init{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    if (isOSSupported && self.secureTextEntry)
    {
        // Listen for changes.
        [self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
        [self addTarget:self action:@selector(editingDidChange) forControlEvents:UIControlEventEditingChanged];
        [self addTarget:self action:@selector(editingDidFinish) forControlEvents:UIControlEventEditingDidEnd];
    }
}

-(void)setText:(NSString *)text{
    [super setText:text];
    if (isOSSupported && !self.editing && self.secure) {
        self.actualText = text;
        [self editingDidFinish];
    }
}

-(NSString*)text
{
    if (!isOSSupported) {
        return super.text;
    }
    
    if (self.editing || self.secure == NO || self.actualText == nil)
    {
        return super.text;
    }
    else
    {
        return self.actualText;
    }
}

-(void)editingDidBegin
{
    self.secureTextEntry = YES;
    super.text = self.actualText;
}

-(void)editingDidChange
{ self.actualText = self.text; }

-(void)editingDidFinish
{
    self.secureTextEntry = NO;
    self.actualText = self.text;
    super.text = [self dotPlaceholder];
}

-(NSString*)dotPlaceholder
{
    int index = 0;
    NSMutableString *dots = @"".mutableCopy;
    while (index < self.text.length)
    { [dots appendString:@"•"]; index++; }
    return dots;
}

@end
